package org.szy.core.common.entity;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.http.useragent.UserAgent;
import cn.hutool.http.useragent.UserAgentUtil;
import org.noear.solon.core.handle.Context;
import org.szy.core.common.uilts.IpAddressUtil;

import java.io.Serializable;
import java.util.List;

/**
 * 用户信息
 *
 * @author 孙志阳
 * @since 2.0.0
 */
public class LoginUser implements Serializable {

    /**
     * 用户唯一标识（短期）
     */
    private String token;

    /**
     * 用户名id
     */
    private Object userId;

    /**
     * 用户名/登录名
     */
    private String userName;

    /**
     * 设备标识 PC/APP/H5/WX
     */
    private String device;

    /**
     * 用户类型
     * {@link org.szy.core.common.enums.UserTypeEnum}
     */
    private String userType;

    /**
     * 展示名/实际名
     */
    private String name = "佚名";

    /**
     * 用户头像
     */
    private String avatar;

    /**
     * 登录时间
     */
    private Long loginTime;

    /**
     * 过期时间
     */
    private Long expireSecond;

    /**
     * 创建时间
     */
    private Long createTime;

    /**
     * 登录IP地址
     */
    private String ipAddr;

    /**
     * 登录地点
     */
    private String loginLocation;

    /**
     * 浏览器类型
     */
    private String browser;

    /**
     * 操作系统
     */
    private String os;

    /**
     * 权限列表
     */
    private List<String> permissions;

    /**
     * 角色列表
     */
    private List<String> roles;


    /**
     * 构建登录信息
     *
     * @param loginUser 登录信息
     */
    public void builderLoginUser(LoginUser loginUser) {
        Context ctx = Context.current();
        loginUser.setIpAddr(ctx.realIp());
        UserAgent userAgent = UserAgentUtil.parse(ctx.header("User-Agent"));
        loginUser.setLoginLocation(IpAddressUtil.getCityInfo(ctx.realIp()));
        loginUser.setBrowser(userAgent.getBrowser().getName());
        loginUser.setOs(userAgent.getOs().getName());
        loginUser.setLoginTime(System.currentTimeMillis());
        loginUser.setDevice(userAgent.isMobile() ? "PHONE" : "PC");
        SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
        loginUser.setToken(tokenInfo.getTokenValue());
    }


    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public Object getUserId() {
        return userId;
    }

    public void setUserId(Object userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getDevice() {
        return device;
    }

    public void setDevice(String device) {
        this.device = device;
    }

    public String getUserType() {
        return userType;
    }

    public void setUserType(String userType) {
        this.userType = userType;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }

    public Long getLoginTime() {
        return loginTime;
    }

    public void setLoginTime(Long loginTime) {
        this.loginTime = loginTime;
    }

    public String getIpAddr() {
        return ipAddr;
    }

    public void setIpAddr(String ipAddr) {
        this.ipAddr = ipAddr;
    }

    public String getLoginLocation() {
        return loginLocation;
    }

    public void setLoginLocation(String loginLocation) {
        this.loginLocation = loginLocation;
    }

    public String getBrowser() {
        return browser;
    }

    public void setBrowser(String browser) {
        this.browser = browser;
    }

    public String getOs() {
        return os;
    }

    public void setOs(String os) {
        this.os = os;
    }

    public List<String> getPermissions() {
        return permissions;
    }

    public void setPermissions(List<String> permissions) {
        this.permissions = permissions;
    }

    public List<String> getRoles() {
        return roles;
    }

    public void setRoles(List<String> roles) {
        this.roles = roles;
    }

    public Long getExpireSecond() {
        return expireSecond;
    }

    public void setExpireSecond(Long expireSecond) {
        this.expireSecond = expireSecond;
    }

    public Long getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Long createTime) {
        this.createTime = createTime;
    }
}
